home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr26 / netprog.zip / NETPROG.TAR / fd / sendfile.c < prev   
C/C++ Source or Header  |  1989-12-17  |  788b  |  33 lines

  1. /*
  2.  * Pass a file descriptor to another process.
  3.  * Return 0 if OK, otherwise return the errno value from the
  4.  * sendmsg() system call.
  5.  */
  6.  
  7. #include    <sys/types.h>
  8. #include    <sys/socket.h>
  9. #include    <sys/uio.h>
  10.  
  11. int
  12. sendfile(sockfd, fd)
  13. int    sockfd;        /* UNIX domain socket to pass descriptor on */
  14. int    fd;        /* the actual fd value to pass */
  15. {
  16.     struct iovec    iov[1];
  17.     struct msghdr    msg;
  18.     extern int    errno;
  19.  
  20.     iov[0].iov_base = (char *) 0;        /* no data to send */
  21.     iov[0].iov_len  = 0;
  22.     msg.msg_iov          = iov;
  23.     msg.msg_iovlen       = 1;
  24.     msg.msg_name         = (caddr_t) 0;
  25.     msg.msg_accrights    = (caddr_t) &fd;    /* address of descriptor */
  26.     msg.msg_accrightslen = sizeof(fd);    /* pass 1 descriptor */
  27.  
  28.     if (sendmsg(sockfd, &msg, 0) < 0)
  29.         return( (errno > 0) ? errno : 255 );
  30.  
  31.     return(0);
  32. }
  33.